function emptyfieldCategory(){
	Ext.getCmp('ConfigAssetCategory').getForm().reset();	
}

function SearchAssetCategory(){
	var mainCat = Ext.getCmp("mainCat").getValue();
	var subCat1 = Ext.getCmp("subCat1").getValue();
	var subCat2 = Ext.getCmp("subCat2").getValue();
	var subCat3 = Ext.getCmp("subCat3").getValue();
	var keyword = Ext.getCmp("keyword").getValue();
	Ext.getStore('Administration.AssetCategory').getProxy().url = "./Services/DataMaster/AssetCategory/List?keyword="+keyword+"&MAIN_CATEGORY="+mainCat+"&SUB_CATEGORY_1="+subCat1+"&SUB_CATEGORY_2="+subCat2+"&SUB_CATEGORY_3="+subCat3;
	Ext.getStore('Administration.AssetCategory').load();
}

function SearchAssetCategoryAddCat(){
	var mainCatAddCat = Ext.getCmp("mainCatAddCat").getValue();
	var subCat1AddCat = Ext.getCmp("subCat1AddCat").getValue();
	var subCat2AddCat = Ext.getCmp("subCat2AddCat").getValue();
	var subCat3AddCat = Ext.getCmp("subCat3AddCat").getValue();
	var keywordAddCat = Ext.getCmp("keywordAddCat").getValue();
	Ext.getStore('Administration.AssetCategory').getProxy().url = "./Services/DataMaster/AssetCategory/List?keyword="+keywordAddCat+"&MAIN_CATEGORY="+mainCatAddCat+"&SUB_CATEGORY_1="+subCat1AddCat+"&SUB_CATEGORY_2="+subCat2AddCat+"&SUB_CATEGORY_3="+subCat3AddCat;
	Ext.getStore('Administration.AssetCategory').load();
}

function DeleteAssetCategory(){
	var rec = Ext.getCmp('table_assetCategory').getSelectionModel().getSelection();
	if (rec.length>0){
		  Ext.MessageBox.confirm('Delete', 'Are you sure ?', function(btn) {
		        if (btn === 'yes') {
		        	 for (var i = 0; i<rec.length ; i++){
		     	    	var id = rec[i].get('CATEGORY_ID');
		     	    	var cat = rec[i].get('CATEGORY');
		     	    	var data = {CATEGORY_ID:id, CATEGORY:cat};
		     	    	var url  = './Services/DataMaster/AssetCategory/Delete';
		     	    	PostData(data,url,"Delete");
		     	    }
		         RefreshAssetCategory();
		        
		        }
		    });
	}else{
    	Ext.MessageBox.show({
		 	title: 'WARNING!',
	        msg: 'No Data Selected',
	        icon: Ext.Msg.WARNING
	    });	
    }
}

function getValueGridAssetCategory(record){
	Ext.getCmp('ButtonAddMasterCategory').setText('Update');
	var id  = record.get('CATEGORY_ID');
	if(Ext.getCmp('ConfigAssetCategory').hidden == true) Ext.getCmp('ConfigAssetCategory').toggleCollapse(true);
	Ext.Ajax.request({
	    method: "GET",
	    dataType: "json",
	    url: "./Services/DataMaster/AssetCategory?CATEGORY="+id+"",
	    async: false	,
	    success: function(c) {
	    	var jsonResults= $.parseJSON(c.responseText);
	    	data = jsonResults.item[0];
	    	Ext.getCmp('ConfigAssetCategory').getForm().setValues(data);
	    	Ext.getCmp('CATEGORYtemp').setValue(Ext.getCmp('CATEGORY').getValue());
	        }
	    });	
	
}

function RefreshAssetCategory(){
	Ext.getStore('Administration.AssetCategory').getProxy().url = "./Services/DataMaster/AssetCategory/List";
	Ext.getStore('Administration.AssetCategory').load();
}

function AddCategory() {
	  var win = new Ext.Window({
	        modal : true,
	        height : 0.4 * screen.width,
	        width : 1080,
	        border : false,
	        resizable : false,
	        closable : true,
	        title : 'Asset Category List',
	        layout : 'fit',
	        titleAlign : 'center',
	        frame : true,
	        autoScroll : false,
	        listeners: {
		  		afterrender : function () {
		  			Ext.getStore('AssetDataMaster.MainCat').load();
	        	}
	        
	  		},
	  
	        	
	        items : [
	        {
		            xtype: 'gridpanel',
		            region: 'center',
		            height: 590,
		            width: 400,
		            id : 'table_add_category_tree',
		            titleAlign: 'center',
		            margin: '5 5 5 0',
		            border: true,
		            forceFit: true,
		            columnLines: true,
		            selType: 'checkboxmodel',
		            store: 'Administration.AssetCategory',
		            columns: [
		                {dataIndex: 'MAIN_CATEGORY', text: 'MAIN CATEGORY', align: 'left'},
		                {dataIndex: 'SUB_CATEGORY_1', text: 'SUB CATEGORY 1', align: 'left'},
		                {dataIndex: 'SUB_CATEGORY_2', text: 'SUB CATEGORY 2', align: 'left'},
		                {dataIndex: 'SUB_CATEGORY_3', text: 'SUB CATEGORY 3', align: 'left'},
		                {dataIndex: 'CATEGORY', text: 'CATEGORY', align: 'left'},
		                {dataIndex: 'STATUS', text: 'ACTIVE', align: 'left'}
		            ],
		            dockedItems: [
		                {
		                    xtype: 'pagingtoolbar',
		                    dock: 'bottom',
		                    store: 'Administration.AssetCategory',
		                    displayInfo: true
		                },
		                {
		                    xtype: 'toolbar',
		                    dock: 'top',
		                    items: [
	                            {
				                    xtype: 'combobox',
				                    cls: 'black',
				                    id: 'mainCatAddCat',
				                    name: 'mainCatAddCat',
				                    margin: '10 10 5 5',
		                            width: 100 / 1366 * screen.width,
				                    displayField: 'MAIN_CATEGORY',
				                    valueField: 'MAIN_CATEGORY',
				                    emptyText: 'Main Cat.',
				                    msgTarget: 'title',
				                    queryMode: "local",
				                    allowBlank: true,
				                    forceSelection: true,
				                    store: 'AssetDataMaster.MainCat',
				                    listeners: {
				                        change: function (obj, newValue, oldValue, eOpts) {
				                            var mainCatAddCat = Ext.getCmp("mainCatAddCat").getValue();
				                            Ext.getStore('AssetDataMaster.SubCat1').removeAll();
				                            Ext.getStore('AssetDataMaster.SubCat1').getProxy().url = './Services/DataMaster/SubCat1/List?MAIN_CATEGORY=' + mainCatAddCat;
				                            Ext.getStore('AssetDataMaster.SubCat1').load();
				                        }
				                    }
				                },
				                {
			                        xtype: 'combobox',
			                        cls: 'black',
			                        id: 'subCat1AddCat',
			                        name: 'subCat1AddCat',
			                        margin: '10 10 5 5',
		                            width: 100 / 1366 * screen.width,
			                        displayField: 'SUB_CATEGORY_1',
			                        valueField: 'SUB_CATEGORY_1',
			                        emptyText: 'Sub Cat. 1',
			                        msgTarget: 'title',
			                        queryMode: "local",
			                        allowBlank: true,
			                        forceSelection: true,
			                        store: 'AssetDataMaster.SubCat1',
			                        listeners: {
			                            change: function (obj, newValue, oldValue, eOpts) {
			                                var subCat1AddCat = Ext.getCmp("subCat1AddCat").getValue();
			                                Ext.getStore('AssetDataMaster.SubCat2').removeAll();
			                                Ext.getStore('AssetDataMaster.SubCat2').getProxy().url = './Services/DataMaster/SubCat2/List?SUB_CATEGORY_1=' + subCat1AddCat;
			                                Ext.getStore('AssetDataMaster.SubCat2').load();
			                            }
			                        }
			                    },
			                    {
			                        xtype: 'combobox',
			                        cls: 'black',
			                        id: 'subCat2AddCat',
			                        name: 'subCat2AddCat',
			                        margin: '10 10 5 5',
		                            width: 100 / 1366 * screen.width,
			                        displayField: 'SUB_CATEGORY_2',
			                        valueField: 'SUB_CATEGORY_2',
			                        emptyText: 'Sub Cat. 2',
			                        msgTarget: 'title',
			                        queryMode: "local",
			                        allowBlank: true,
			                        forceSelection: true,
			                        store: 'AssetDataMaster.SubCat2',
			                        listeners: {
			                            change: function (obj, newValue, oldValue, eOpts) {
			                                var subCat2AddCat = Ext.getCmp("subCat2AddCat").getValue();
			                                Ext.getStore('AssetDataMaster.SubCat3').removeAll();
			                                Ext.getStore('AssetDataMaster.SubCat3').getProxy().url = './Services/DataMaster/SubCat3/List?SUB_CATEGORY_2=' + subCat2AddCat;
			                                Ext.getStore('AssetDataMaster.SubCat3').load();
			                            }
			                        }
			                    },
			                    {
			                        xtype: 'combobox',
			                        cls: 'black',
			                        id: 'subCat3AddCat',
			                        name: 'subCat3AddCat',
			                        margin: '10 10 5 5',
		                            width: 100 / 1366 * screen.width,
			                        displayField: 'SUB_CATEGORY_3',
			                        valueField: 'SUB_CATEGORY_3',
			                        emptyText: 'Sub Cat. 3',
			                        msgTarget: 'title',
			                        allowBlank: true,
			                        forceSelection: true,
			                        queryMode: "local",
			                        store: 'AssetDataMaster.SubCat3'
			                    },
		                        {
		                            xtype: 'textfield',
		                            emptyText: 'Keyword',
		                            width: 130 / 1366 * screen.width,
		                            id: 'keywordAddCat',
		                            margin: '0 0 0 10',
		                            enableKeyEvents: true,
		                            listeners: {
		                                keypress: function (form, e) {
		                                    if (e.getKey() == 13) {
		                                    	SearchAssetCategoryAddCat();
		                                    }
		
		                                }
		                            }
		                        },
		                        {
		                            xtype: 'button',
		                            text: 'SEARCH',
		                            margin: '0 10 0 0',
		                            handler: function () {
		                            	SearchAssetCategoryAddCat();
		                            }
		                        },
		                        {
		                            xtype: 'button',
		                            text: 'ADD THIS CATEGORY INTO SITE',
		                            margin: '0 0 0 0',
		                            handler: function () {
		                            	var rec = Ext.getCmp('table_add_category_tree').getSelectionModel().getSelection();
		                            	if (rec.length>0){
		            		        	 for (var i = 0; i<rec.length ; i++){
		            		     	    	var id = rec[i].get('CATEGORY_ID');
		            		     	    	var data = {SITE_ID:site_id, CATEGORY_ID:id};
		            		     	    	var url  = "./Services/Asset/Site/Categories/Add";
		            		     	    	PostData(data,url,"Add Data Category");
		            		     	    	
		            		     	    }
		            		        
		            		        	  try {
		            		                    win.destroy();
		            		                  } catch (e) {
		            		                    win.destroy();
		            		                  }
		            		                  
		            		                  Ext.getStore ("AssetDataMaster.Treeview").load();
		                            		   
		                            	}else{
		                                	Ext.MessageBox.show({
		                            		 	title: 'WARNING!',
		                            	        msg: 'No Data Selected',
		                            	        icon: Ext.Msg.WARNING
		                            	    });	
		                                }     
		                            }
		                        }
		                    ]
		                }
		            ],
		            listeners: {
		                afterrender: function () {
		                    Ext.getStore('Administration.AssetCategory').load();
		                },
//		                itemdblclick: function (grid, record) {
//		                    getValueGridAssetCategory(record);
//		                }
		            },
        }]

	      }).show();

	}

	

function deleteCategory() {
	  var win1 = new Ext.Window({
	        modal : true,
	        height : 0.3 * screen.width,
	        width : 700,
	        border : false,
	        resizable : false,
	        closable : true,
	        title : 'Asset Category Site',
	        layout : 'fit',
	        titleAlign : 'center',
	        frame : true,
	        autoScroll : false,
	        items : [
	        {
		            xtype: 'gridpanel',
		            region: 'center',
		            height: 590,
		            width: 400,
		            id : 'table_delete_category_tree',
		            titleAlign: 'center',
		            margin: '5 5 5 0',
		            border: true,
		            forceFit: true,
		            columnLines: true,
		            selType: 'checkboxmodel',
		            store: 'Administration.SiteHierarchy',
		            columns: [
		                {dataIndex: 'SITE_ID', text: 'SITE ID', align: 'left'},
		                {dataIndex: 'CATEGORY', text: 'CATEGORY', align: 'left'},
		                {dataIndex: 'STATUS', text: 'ACTIVE', align: 'left'}
		            ],
		            dockedItems: [
		                {
		                    xtype: 'pagingtoolbar',
		                    dock: 'bottom',
		                    store: 'Administration.SiteHierarchy',
		                    displayInfo: true
		                },
		                {
		                    xtype: 'toolbar',
		                    dock: 'top',
		                    items: [
		                       
		                        {
		                            xtype: 'button',
		                            text: 'DELETE THIS CATEGORY FROM SITE',
		                            margin: '0 0 0 0',
		                            handler: function () {
		                            	var rec = Ext.getCmp('table_delete_category_tree').getSelectionModel().getSelection();
		                            	if (rec.length>0){
		                            		Ext.MessageBox.confirm('Delete', 'Are you sure ?', function(btn) {
		                        		        if (btn === 'yes') {
		                        		        	for (var i = 0; i<rec.length ; i++){
					            		     	    	var id = rec[i].get('CATEGORY_ID');
					            		     	    	var data = {SITE_ID:site_id, CATEGORY_ID:id};
					            		     	    	var url  = "./Services/Asset/Site/Categories/Delete";
					            		     	    	PostData(data,url,"Delete Category");						            		     	    	
					            		     	    }
						            		        
		                        		        	try {
				            		                    win1.destroy();
		                        		        	} catch (e) {
				            		                    win1.destroy();
		                        		        	}						            		                  
		                        		        	Ext.getStore ("AssetDataMaster.Treeview").load();
		                        		        }
	                        		        });		
		                            	}else{
		                                	Ext.MessageBox.show({
		                            		 	title: 'WARNING!',
		                            	        msg: 'No Data Selected',
		                            	        icon: Ext.Msg.WARNING
		                            	    });	
		                                }     
		                            }
		                        }
		                    ]
		                }
		            ],
		            listeners: {
		                beforerender: function () {
	                		Ext.getStore('Administration.SiteHierarchy').removeAll();
                            Ext.getStore('Administration.SiteHierarchy').getProxy().url = './Services/DataMaster/SiteHierarchy/List?keyword=' + site_id;
                            
		                    Ext.getStore('Administration.SiteHierarchy').load();
		                },
		            },
        }]

	      }).show();

	}
